package display
{
	import events.CScrollPaneEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import skinfactory.SkinButton;
	import skinfactory.SkinScaleImage;
		/**
		 * ■ 사용자 정의 스크롤바 
		 * 
		 **/
	public class CScrollPane extends Sprite
	{
		public var btnUP: SkinButton;
		public var btnDown: SkinButton;
		private var bar: SkinScaleImage;
		private var vitureBar: Sprite;
		public var bg: SkinScaleImage;
		
		private var firstPosition: int;
		
		private var screen: Sprite;
		
		private var scrollObj: Sprite;
		
		private var dragRect: Rectangle;
		
		public var initPos: Boolean;
		
		public function CScrollPane()
		{
			this.init();
		}
		
		private function init(): void
		{
			screen = new Sprite();
			screen.graphics.beginFill( 0xFF0000, 0.5 );
			screen.graphics.drawRect( 0, 0, 100, 100 );
			screen.graphics.endFill();
			this.addChild( screen );
			
			scrollObj = new Sprite();
			scrollObj.mask = screen;
			
			btnUP = new SkinButton( "scrollPane_btnUp");
			btnUP.addEventListener( MouseEvent.CLICK, onMouseEventHanlder );
			
			btnDown = new SkinButton(  "scrollPane_btnDown" );
			btnDown.addEventListener( MouseEvent.CLICK, onMouseEventHanlder );
			
			bar = new SkinScaleImage( "scrollPane_bar" );
			vitureBar = new Sprite();
			vitureBar.graphics.beginFill( 0xFF0000, 0 );
			vitureBar.graphics.drawRect( 0, 0, 100, 100 );
			vitureBar.graphics.endFill();
			vitureBar.addEventListener( MouseEvent.MOUSE_DOWN, onBarMouseEvent );
			this.addEventListener( Event.ADDED_TO_STAGE, onAddToStage );
			vitureBar.buttonMode = true;
			
			bg = new SkinScaleImage( "scrollPane_bg" );
			
			initPos = false;
			
			this.addEventListener( MouseEvent.MOUSE_WHEEL, onMouseWheelHanlder );
			
			this.relocate();
		}
		// 각종 버튼의 스킨을 변경 
		public function changeSkin( _btnUp: String, _btnDown: String, _bar: String, _bg: String ): void
		{
			if( this.contains( btnUP ) ) this.removeChild( btnUP );
			if( this.contains( btnDown ) ) this.removeChild( btnDown );
			if( this.contains( bar ) ) this.removeChild( bar );
			if( this.contains( bg ) ) this.removeChild( bg );
			if( this.contains( vitureBar ) ) this.removeChild( vitureBar );
			
			btnUP = new SkinButton( _btnUp );
			btnUP.addEventListener( MouseEvent.CLICK, onMouseEventHanlder );
			
			btnDown = new SkinButton( _btnDown );
			btnDown.addEventListener( MouseEvent.CLICK, onMouseEventHanlder );
			
			bar = new SkinScaleImage( _bar );
			bg = new SkinScaleImage( _bg );
			
			this.relocate();
		}
		public function skinChange( _bg: SkinScaleImage, _btnUp: SkinButton, _btnDown: SkinButton, _bar: SkinScaleImage ): void
		{
			if( this.contains( bg ) ) this.removeChild( bg );
			if( this.contains( btnUP ) ) this.removeChild( btnUP );
			if( this.contains( btnDown ) ) this.removeChild( btnDown );
			if( this.contains( bar ) ) this.removeChild( bar );
			
			bg = _bg;
			
			this.relocate();
		} 
		//각종 컨트롤 의 위치 셋팅 
		private function relocate(): void
		{
			btnUP.x = screen.width - btnUP.width;
			btnUP.y = screen.y;
			
			btnDown.x = screen.width - btnDown.width;
			btnDown.y = screen.y + screen.height - btnDown.height;
			
			bar.x = btnUP.x + ( btnUP.width - ( btnUP.width + bar.width ) / 2 )
			bar.y = btnUP.y + btnUP.height;
			bar.height = screen.height - btnUP.height - btnDown.height;
			
			vitureBar.x = screen.width - bar.width;
			vitureBar.y = btnUP.y + btnUP.height;
			firstPosition = vitureBar.y;
			vitureBar.height = screen.height - btnUP.height - btnDown.height;
			vitureBar.width = bar.width;
			
			dragRect = new Rectangle();
			dragRect.x = bar.x;
			dragRect.y = bar.y;
			dragRect.width = 0;
			dragRect.height = bar.height;
			
			bg.x = screen.width - bg.width;
			bg.height = screen.height;

			if( !contains( scrollObj ) ) this.addChild( scrollObj );
			if( !contains( bg ) ) this.addChild( bg );
			if( !contains( btnUP ) ) this.addChild( btnUP );
			if( !contains( btnDown ) ) this.addChild( btnDown );
			if( !contains( bar ) ) this.addChild( bar );
			if( !contains( vitureBar ) ) this.addChild( vitureBar );
		} 
		// 스크롤바 영역 설정
		public function setSize( width: uint, height: uint ): void
		{
			screen.width = width;
			screen.height = height;
			
			this.relocate();
		}
		override public function set width(value:Number):void
		{
			screen.width = value;
			
			this.relocate();
		}
		override public function set height(value:Number):void
		{
			screen.height = value;
			
			this.relocate();
		}
		//스크롤바 될 객체 셋팅 
		public function set child( obj: DisplayObject ): void
		{
			bar.height = screen.height - btnUP.height - btnDown.height;
			dragRect.height = bar.height;
			this.scrollObjInit();
			scrollObj.addChild(obj as DisplayObject);
			if( initPos )
				scrollObj.y = 0;
			if( obj.height > screen.height )
			{
				//현재 이미지의 몇퍼센트가 보여지는지 계산
				var rate: Number = screen.height  / obj.height;
				
				bar.height = int( bar.height * rate );
				//vitureBar.height = vitureBar.height * rate;
				vitureBar.height = bar.height;
				dragRect.height -= bar.height;
				
				if( initPos )
				{
					bar.y = btnUP.y + btnUP.height;
					vitureBar.y = bar.y;
				}
				
				btnDown.visible 	= true;
				btnUP.visible 		= true;
				bar.visible 		= true;
				bg.visible 			= true;
				vitureBar.visible 	= true;
				this.dispatchEvent( new CScrollPaneEvent( CScrollPaneEvent.ACTIVE, bg.width, true ) );
			}
			else
			{
				vitureBar.visible = false;
				btnDown.visible = false;
				btnUP.visible = false;
				bar.visible = false;
				bg.visible = false;
				this.dispatchEvent( new CScrollPaneEvent( CScrollPaneEvent.DEACTIVE, bg.width, true ) );
			}
		}
		// 스크롤바 초기화 
		private function scrollObjInit(): void
		{
			for( var i: int = 0; i < scrollObj.numChildren; i++ )
			{
				scrollObj.removeChild( scrollObj.getChildAt(i) );
			}
		}
		//스크롤바 마우스 이벤트 
		private function onMouseEventHanlder( e: MouseEvent ): void
		{
			switch( e.currentTarget ) 
			{
				case btnUP:
					if( firstPosition > vitureBar.y - 20 )
					{
						vitureBar.y = firstPosition;
					}
					else
					{
						vitureBar.y -= 20;	
					}
					break;
				case btnDown:
					if( vitureBar.y + 20 > btnDown.y - vitureBar.height )
					{
						vitureBar.y = btnDown.y - vitureBar.height;
					}
					else
					{
						vitureBar.y += 20;
					}
					break;
			}
			var rate: Number = ( vitureBar.y - dragRect.y ) / dragRect.height;
			bar.y = vitureBar.y;
			scrollObj.y = - ( rate * ( scrollObj.height - screen.height ) );
		}
		public function update(): void
		{
			trace("updata");
			vitureBar.y = btnDown.y - vitureBar.height;
			var rate: Number = ( vitureBar.y - dragRect.y ) / dragRect.height;
			bar.y = vitureBar.y;
			scrollObj.y = - ( rate * ( scrollObj.height - screen.height ) );
		}
		private function onBarMouseEvent( e: MouseEvent ): void
		{
			switch( e.type )
			{
				case MouseEvent.MOUSE_DOWN:
					this.onBarMouseDown();
					break;
				case MouseEvent.MOUSE_UP:
					this.onBarMouseUp();
					break;
			}
		}
		//마우스 휠을 사용했을때의 이벤트 핸들러 
		private function onMouseWheelHanlder ( e: MouseEvent ): void
		{
			if( vitureBar.y - (5*(e.delta)) > btnDown.y - vitureBar.height )
			{
				vitureBar.y = btnDown.y - vitureBar.height;
			}
			else if( vitureBar.y - (5*(e.delta)) < firstPosition )
			     vitureBar.y = firstPosition;
			else vitureBar.y += -(5*(e.delta));
			
			bar.y = vitureBar.y;
			var rate: Number = ( vitureBar.y - dragRect.y ) / dragRect.height;
			scrollObj.y = - ( rate * ( scrollObj.height - screen.height ) );
		}
		// 스크롤바를 드래그 
		private function onBarMouseDown(): void
		{
			//bar.startDrag( false, dragRect );
			vitureBar.startDrag( false, dragRect );
			this.addEventListener( Event.ENTER_FRAME, onEnterFrameListener );
		}
		private function onBarMouseUp(): void
		{
			//bar.stopDrag();
			vitureBar.stopDrag();
			this.removeEventListener( Event.ENTER_FRAME, onEnterFrameListener );
		}
		private function onEnterFrameListener( e: Event ): void
		{
			var rate: Number = ( vitureBar.y - dragRect.y ) / dragRect.height;
			bar.y = vitureBar.y;
			scrollObj.y = - ( rate * ( scrollObj.height - screen.height ) );			
		}
		private function onAddToStage( e: Event ): void
		{
			stage.addEventListener( MouseEvent.MOUSE_UP, onBarMouseEvent );
		}
	}
}
